﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nascenture.Models;
using System.Web.Mail;
using System.Net.Mail;
using System.Net;
using System.Collections;
using System.Web.Hosting;
using System.IO;

namespace Nascenture.Controllers
{
    public class ContactController:Controller
    {
        private NascentureDBContext db = new NascentureDBContext();
        public ActionResult Contact()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Contact(Contact ContactDB)
        {
            if (ModelState.IsValid)
            {
                SendEmail(ContactDB.Subject, ContactDB.EmailID, ContactDB.Name + " <br>" + ContactDB.Phone + "<br>" + ContactDB.Message);
                db.Contact.Add(ContactDB);
                db.SaveChanges();
                return View();
            }

            return View();
        }
        //public Int32 SendMail(String To, String From, String Subject, String Body)
        //{
        //    try
        //    {
        //        MailMessage msgMail = new MailMessage();
        //        msgMail.To = To;
        //        SmtpMail.SmtpServer = "mail.google.com";
        //        msgMail.From = From;
        //        msgMail.Subject = Subject;
        //        msgMail.BodyFormat = MailFormat.Html;
        //        msgMail.Body = Body;
        //        SmtpMail.Send(msgMail);
        //        return 1;
        //    }
        //    catch (Exception)
        //    {

        //        return -1;
        //    }



        //}
       

        public Boolean SendEmail(string Subj, string sendEmail, string Msg)
        {

            Boolean result = false;
            string emailFrom = sendEmail;
            string emailTo = System.Configuration.ConfigurationManager.AppSettings["MailFrom"].ToString();;
            string SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SMTPServer"].ToString();
            string userid = System.Configuration.ConfigurationManager.AppSettings["SmtpUsername"].ToString();
            string pwd = System.Configuration.ConfigurationManager.AppSettings["SmtpPassword"].ToString();
            string portnumber = System.Configuration.ConfigurationManager.AppSettings["Portnumber"].ToString();

            System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage(emailTo,emailFrom);

            objMail.Subject = Subj;
            objMail.Body = Msg;
            objMail.Bcc.Add(emailTo);

            SmtpClient Client = new SmtpClient(SmtpServer, int.Parse(portnumber));

            System.Net.NetworkCredential basicAuthenticationInfo = new NetworkCredential(userid, pwd);
            Client.UseDefaultCredentials = false;
            Client.Credentials = basicAuthenticationInfo;
            //Client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

            Client.Host = SmtpServer;
            objMail.IsBodyHtml = true;
            try
            {


                Client.Send(objMail);


                return true;
            }
            catch (Exception ex)
            {
                return false;
                //throw new InvalidCastException(ex.Message);
            }




        }
    }
}